<script setup>
import { darkTheme, useMessage} from "naive-ui"
import Session from "./session.js"
import { ref} from "vue"
import { Sess } from "./user.js"

const messager = useMessage()
const login = defineModel("login")
const logined = defineModel("logined")
const chatids = defineModel("chatids")
const chats = defineModel("chats")

const sessionHandler = ref(null);
const user = new Sess()
if (user.valid()){
  logined.value = true
  sessionHandler.value = new Session(user.name)
  sessionHandler.value.load((data2)=>{
    // console.log("hallo2:",data2)
    for(let k in data2.sessions){
      chatids.value.push(k);
      chats.value[k] = data2.sessions[k];
    }
    messager.success("载入会话："+chatids.value.length)
  })
}
const generate = ()=>{
  user.name = login.value.username
  user.save()
      // messager.success("登录成功")
  logined.value = true
  sessionHandler.value = new Session(user.name)
  sessionHandler.value.load((data2)=>{
    // console.log("hallo2:",data2)
    for(let k in data2.sessions){
      chatids.value.push(k);
      chats.value[k] = data2.sessions[k];
    }
    messager.success("载入会话："+data2.sessions.length)
  })
  
}

// const to_regist = ()=>{
//   fetch("/register", {
//     method: "POST",
//     headers: {
//       "Content-Type": "application/json"
//     },
//     body: JSON.stringify({
//       username:login.value.username,
//       password:login.value.password,
//       code:login.value.code,
//     })
//   }).then(d=>d.json()).then(data=>{
//     console.log("dd:",data)
//     if (data.message != null){
//       alert("注册成功")
//       location.reload()
//     }else{
//       alert(data.error)
//     }
//   })
// }
// const to_login = ()=>{
//   fetch("/login", {
//     method: "POST",
//     headers: {
//       "Content-Type": "application/json"
//     },
//     body: JSON.stringify(login.value)
//   }).then(d=>d.json()).then(data=>{
//     console.log("dd:",data)
//     if (data.message != null){
//       // messager.success("登录成功")
//       logined.value = true
//       sessionHandler.value = new Session(login.value.username)
//       sessionHandler.value.load((data2)=>{
//         console.log("hallo2:",data2)
//         for(let k in data2.sessions){
//           chatids.value.push(k);
//           chats.value[k] = data2.sessions[k];
//         }
//       })
//     }else{
//       alert(data.error)
//     }
//   })
// }

</script>

<template>
    <!-- <n-card  style="position:fixed; top:20%;left:33%; width:400px; margin:20px;padding:20px;border-radius:20px;height:500px"> -->
        <n-tabs   type="line" animated style="position:fixed; top:20%;left:33%; width:400px; margin:20px;padding:20px;border-radius:20px;height:320px;padding-left:10px;background: #1e1e1e" :theme="darkTheme" >
            <n-tab-pane name="login" tab="登录">
            <n-space vertical >
                <n-input-group>
                <n-input-group-label>用户</n-input-group-label>
                <n-input v-model:value="login.username"></n-input>
                </n-input-group>
                <!-- <n-input-group>
                  <n-input-group-label>密码</n-input-group-label>
                  <n-input type="password" @keyup.enter="to_login" v-model:value="login.password"></n-input>
                </n-input-group> -->
                <n-button style="width:100%" @click="generate">保存</n-button>
            </n-space>
            </n-tab-pane>
            <!-- <n-tab-pane name="regist" tab="注册">
            <n-space vertical >
                <n-input-group>
                <n-input-group-label>用户名</n-input-group-label>
                <n-input v-model:value="login.username"></n-input>
                </n-input-group>
                <n-input-group>
                <n-input-group-label>密码</n-input-group-label>
                <n-input type="password" @keyup.enter="to_login" v-model:value="login.password"></n-input>
                </n-input-group>
                <n-input-group>
                <n-input-group-label>注册码</n-input-group-label>
                <n-input type="password" @keyup.enter="to_login" v-model:value="login.code"></n-input>
                </n-input-group>
                <n-button style="width:100%" @click="to_regist">注册</n-button>
            </n-space>
            </n-tab-pane> -->
        </n-tabs>            
    <!-- </n-card> -->
</template>